package com.jstyle.beatecg.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jstyle.beatecg.R;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ProgresFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ProgresFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.ComboLineColumnChartView_activity)
    LineChartView ComboLineColumnChartViewActivity;
    Unbinder unbinder;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.tv_time_hard)
    TextView tvTimeHard;
    @BindView(R.id.progressBar1)
    ProgressBar progressBar1;
    @BindView(R.id.tv_time_no)
    TextView tvTimeNo;
    @BindView(R.id.progressBar2)
    ProgressBar progressBar2;
    @BindView(R.id.tv_time_o)
    TextView tvTimeO;
    @BindView(R.id.progressBar3)
    ProgressBar progressBar3;
    @BindView(R.id.tv_time_ranzhi)
    TextView tvTimeRanzhi;
    @BindView(R.id.progressBar4)
    ProgressBar progressBar4;
    @BindView(R.id.tv_time_warm)
    TextView tvTimeWarm;
    private float numberOfPoints = 20;
    LinkedList<Float> queueHeart = new LinkedList<>();
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public ProgresFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     *  param1 Parameter 1.
     *  param2 Parameter 2.
     * @return A new instance of fragment ProgresFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ProgresFragment newInstance() {
        ProgresFragment fragment = new ProgresFragment();
//        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
//        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_progres, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        resetViewport();
        //generateValues();
        generateData();
    }

    private void generateData() {
        // Chart looks the best when line data and column data have similar maximum viewports.
        LineChartData data = generateLineData();
        Axis axisX = new Axis();
        Axis axisYRight = new Axis();
        Axis axisY = new Axis();
        List<AxisValue> axisValues = new ArrayList<>();
        List<AxisValue> axisValuesX = new ArrayList<>();
        axisValues.add(new AxisValue(80, String.valueOf(80).toCharArray()));
        for (int i = 0; i < 7; i++) {
            axisValues.add(new AxisValue(i * 20 + 76, String.valueOf(i * 20 + 80).toCharArray()));
        }
        for (int i = 0; i < 10; i++) {
            axisValuesX.add(new AxisValue(i * 60, "".toCharArray()));
        }
        axisY.setTextSize(10);
        axisX.setTextSize(8);
        axisY.setValues(axisValues);
        data.setAxisXBottom(axisX.setValues(axisValuesX));
        data.setAxisYLeft(axisY);

        ComboLineColumnChartViewActivity.setLineChartData(data);
    }


    private void resetViewport() {
        Viewport v = ComboLineColumnChartViewActivity.getCurrentViewport();
        v.bottom = 80;
        v.top = 200;
        v.left = 0;
        v.right = numberOfPoints;

        ComboLineColumnChartViewActivity.setMaximumViewport(v);
        ComboLineColumnChartViewActivity.setCurrentViewport(v, false);
        ComboLineColumnChartViewActivity.setViewportCalculationEnabled(false);//自适应
        ComboLineColumnChartViewActivity.setZoomType(ZoomType.HORIZONTAL);
        ComboLineColumnChartViewActivity.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
    }

    private Line getHeartDataLine() {
        List<PointValue> values = new ArrayList<PointValue>();
        for (int j = 0; j < queueHeart.size(); ++j) {
            values.add(new PointValue(j, queueHeart.get(j)));
        }
        Line line = new Line(values);
        line.setColor(getResources().getColor(R.color.history_linechart_color));
        line.setCubic(true);
        line.setHasLabels(false);
        line.setHasLines(true);

        line.setHasPoints(false);
        return line;
    }

    private Line getBreathDataLine() {
        List<PointValue> values = new ArrayList<PointValue>();
        for (int j = 0; j < numberOfPoints; ++j) {
            values.add(new PointValue(j, queueHeart.get(j)));
        }
        Line line = new Line(values);
        line.setColor(getResources().getColor(R.color.history_linechart_color));
        line.setCubic(true);
        line.setHasLabels(false);
        line.setHasLines(true);
        line.setHasPoints(false);
        return line;
    }

    private LineChartData generateLineData() {
        List<Line> lines = new ArrayList<Line>();
        lines.add(getHeartDataLine());
        LineChartData lineChartData = new LineChartData(lines);

        return lineChartData;

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
